/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.directive;

import com.jfinal.aop.Inject;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.utils.DateUtil;
import io.jboot.utils.ModelUtil;
import io.jboot.utils.StrUtil;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jpress.model.SiteInfo;
import io.jpress.service.SiteInfoService;
import io.jpress.service.UtmService;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 */
@JFinalDirective("sitePVStatistics")
public class SitePVStatisticsDirective extends JbootDirectiveBase {

    @Inject
    private UtmService utmService;

    @Inject
    private SiteInfoService siteInfoService;


    @Override
    public void onRender(Env env, Scope scope, Writer writer) {

        String excludeSites = getParaToString("excludeSites", scope);


        List<SiteInfo> allSites = ModelUtil.copy(siteInfoService.findAll());

        SiteInfo mainSite = new SiteInfo();
        mainSite.setName("主站点");
        mainSite.setSiteId(0L);
        allSites.add(mainSite);


        if (StrUtil.isNotBlank(excludeSites)) {
            Set<String> excludeSiteSet = StrUtil.splitToSetByComma(excludeSites);
            allSites = allSites.stream().filter(siteInfo -> !excludeSiteSet.contains(String.valueOf(siteInfo.getSiteId()))).collect(Collectors.toList());
        }


        for (SiteInfo siteInfo : allSites) {

            Long weekPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisWeek(), null);
            Long monthPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisMonth(), null);
            Long quarterPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisQuarter(), null);
            Long yearPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisYear(), null);

            siteInfo.put("weekPV",weekPV);
            siteInfo.put("monthPV",monthPV);
            siteInfo.put("quarterPV",quarterPV);
            siteInfo.put("yearPV",yearPV);
        }


        allSites.sort((o1, o2) -> (int) (o2.getLong("weekPV") - o1.getLong("weekPV")));
        scope.setLocal("weekSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("monthPV") - o1.getLong("monthPV")));
        scope.setLocal("monthSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("quarterPV") - o1.getLong("quarterPV")));
        scope.setLocal("quarterSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("yearPV") - o1.getLong("yearPV")));
        scope.setLocal("yearSites",ModelUtil.copy(allSites));

        renderBody(env, scope, writer);
    }




    @Override
    public boolean hasEnd() {
        return true;
    }
}
